package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.CloudContact;
import com.lzx.iteam.bean.PhoneNumItem;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PhoneNumberArea;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hfx on 15/3/10.
 */
public class GetCloudContactMsg extends CcMsgStructure {
    private Context mmContext;
    private Message mmCallback;
    private PhoneNumberArea mPhoneNumberArea;

    public GetCloudContactMsg(Context context,Message message){
        mmContext = context;
        mmCallback = message;
        mPhoneNumberArea = PhoneNumberArea.getInstance(mmContext);
    }

    @Override
    public void onReceive(JSONObject jsonObject) {
        CloudContact contact = new CloudContact();
        try {
            JSONObject jContact = jsonObject.getJSONObject("data");
            contact.groupId = jContact.getString(AsynHttpClient.KEY_GROUP_ID);
            contact.userId = jContact.getString(AsynHttpClient.KEY_USER_ID);
            contact.userType = jContact.getString(AsynHttpClient.KEY_USER_TYPE);
            contact.contactName = jContact.getString(AsynHttpClient.KEY_CONTACT_NAME);
            contact.contact_image = jContact.getString(AsynHttpClient.KEY_CONTACT_IMAGE);
            contact.contact_card = jContact.getJSONObject(AsynHttpClient.KEY_CONTACT_CLOUD_NAMECARD).toString();
            contact.contactRemarks = jContact.getString(AsynHttpClient.KEY_CONTACT_REMARKS);
            contact.contact_sex = jContact.getInt(AsynHttpClient.KEY_SEX);
            if(jContact.has("phone_purview")){
                contact.phone_purview= jContact.getString(AsynHttpClient.KEY_PHONE_PURVIEW);
            }else{
                contact.phone_purview= "1";
            }
            if(jContact.has("calendar_purview")){
                contact.calendar_purview=jContact.getString("calendar_purview");
            }else{
                contact.calendar_purview="0";
            }

            contact.contactPhoneNum = new ArrayList<PhoneNumItem>();
           /* String jNumListStr = jContact.getString(AsynHttpClient.KEY_CONTACT_PHONENUMBER);
            if (!TextUtils.isEmpty(jNumListStr)) {
                JSONArray jNumListArr = new JSONArray(jNumListStr);
                for (int j = 0; j < jNumListArr.length(); j += 2) {
                    PhoneNumItem phoneItem = new PhoneNumItem();
                    phoneItem.label = jNumListArr.getString(j);
                    phoneItem.phoneNum = jNumListArr.getString(j + 1);
                    contact.contactPhoneNum.add(phoneItem);
                }
            }
*/
            contact.contactId = jContact.getString(AsynHttpClient.KEY_CONTACT_ID);
            contact.contact_phone = jContact.getString(AsynHttpClient.KEY_CONTACT_PHONE);
            //contact.contact_landline = jContact.getString(AsynHttpClient.KEY_CONTACT_LANDLINE);

            if (!StringUtil.isEmpty(contact.contact_phone)){
                PhoneNumItem item = new PhoneNumItem();
                item.phoneNum = contact.contact_phone;
                /*String area = mPhoneNumberArea.getArea(contact.contact_phone, true);
                if (!StringUtil.isEmpty(area)){
                    item.label = area;
                }else{
                    item.label = "未知";
                }*/
                item.label = "";
                contact.contactPhoneNum.add(item);
            }

            /*if (!StringUtil.isEmpty(contact.contact_landline)){
                PhoneNumItem item = new PhoneNumItem();
                item.phoneNum = contact.contact_landline;
                String area = mPhoneNumberArea.getArea(contact.contact_landline, true);
                if (!StringUtil.isEmpty(area)){
                    item.label = area;
                }else{
                    item.label = "未知";
                }
                contact.contactPhoneNum.add(item);
            }*/

        }catch (JSONException e){
            e.printStackTrace();
        }
        if(mmCallback != null){
            mmCallback.arg1 = 0;
            mmCallback.obj = contact;
            mmCallback.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
